/*
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License.  You may obtain a
 * copy of the License at the following location:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.jasig.cas.web.flow;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import cn.myfirefly.core.Const;
import cn.myfirefly.utils.VerifyCodeUtils;

/**
 * Performs a basic check if an authentication request for a provided service is authorized to proceed
 * based on the registered services registry configuration (or lack thereof)
 *
 * @author Dmitriy Kopylenko
 * @since 3.5.1
 */
public final class CaptchaCheck extends AbstractController {
	private static final String CAPTCHA_PARAM_NAME = Const.VERIFY_CODE_NAME;//验证码变量名称
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
        ModelAndView mav = new ModelAndView();  
        MappingJacksonJsonView view = new MappingJacksonJsonView();  
        Map<String,Object> attributes = new HashMap<String,Object>(); 
         String captcha  = arg0.getParameter("code");
         if (arg0.getSession().getAttribute(CAPTCHA_PARAM_NAME)==null) {
        	 attributes.put("result", false);
             view.setAttributesMap(attributes);  
             mav.setView(view); 
			return mav;
		}
        boolean valret = VerifyCodeUtils.validate(captcha, 
        		(String)arg0.getSession().getAttribute(CAPTCHA_PARAM_NAME));
        attributes.put("result", valret);
        view.setAttributesMap(attributes);  
        mav.setView(view);  
        return mav;  
	}

	
}
